package com.woniuxy.rabbitmqspringboot.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhangyong
 * @Date 2021/8/12 0012下午 4:13
 */
@Configuration
public class PluginConfiguration {
    //1.创建一个延时队列
    @Bean
    public Queue pluginDelayQueue(){
        return new Queue("plugin_delay_queue");
    }

    //2.创建一个延时交换机
    @Bean
    public CustomExchange customExchange(){
        //指定交换机的初始化参数
        Map<String, Object> map = new HashMap<>();
        //指定当前自定义交换机的类型
        map.put("x-delayed-type","direct");

        //参数2:type消息类型  x-delayed-message   所有的消息都是延时消息
        return new CustomExchange("plugin_delay_exchange","x-delayed-message",true,false,map);

    }

    //绑定交换机
    @Bean
    public Binding bindingPluginDelayQueueToCustomExchange(Queue pluginDelayQueue,CustomExchange customExchange){
        return BindingBuilder
                .bind(pluginDelayQueue)
                .to(customExchange)
                .with("plugin_delay")
                .noargs();//绑定时不指定额外的参数
    }
}
